package com.erp.erp_ui.warehouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.warehouse.req.sellOutStockStatistics1Req;
import com.erp.erp_entitys.warehouse.rsp.CustomerInfoRsp;
import com.erp.erp_entitys.warehouse.rsp.CustomerContactRsp;
import com.erp.erp_entitys.warehouse.rsp.UserInfoRsp;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.AddOtherOutStockReq;
import com.erp.erp_ui.warehouse.req.OtherOutStockListPageReq;
import com.erp.erp_ui.warehouse.req.OtherOutStockProductReq;
import com.erp.erp_ui.warehouse.req.StockInfoReq;
import com.erp.erp_ui.warehouse.service.OtherOutStockService;
import com.erp.erp_ui.warehouse.vo.*;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : OtherOutStockController
 * @Description : 其它出库控制层
 * @Author : lst
 * @Date: 2020-07-29 09:50
 */
@Controller
@RequestMapping(value = "otherOutStock")
public class OtherOutStockController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OtherOutStockController.class);

    /**
     * 其它出库业务逻辑接口
     */
    private final OtherOutStockService otherOutStockService;

    @Autowired
    public OtherOutStockController(OtherOutStockService otherOutStockService) {
        this.otherOutStockService = otherOutStockService;
    }

    /**
     * 首页：获取分页信息
     * @param req 查询条件
     * @return 分页信息
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(OtherOutStockListPageReq req){
        try {
            LOGGER.info("仓库管理-其它出库-首页获取分页信息：req" + req);
            PageInfo<OtherOutStockListPageVo> pageInfo = otherOutStockService.getListByPage(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-首页获取分页信息异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除出库信息
     * @param req 操作对象
     * @return 删除信息
     */
    @PostMapping("/deleteOtherOutStockInfo")
    @ResponseBody
    public R deleteOtherOutStockInfo(@RequestBody OperationReq req){
        try {
            LOGGER.info("仓库管理-其它出库-删除出库信息 req=" + req.toString());
            otherOutStockService.deleteOtherOutStockInfo(req);
            return R.build("");
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-删除出库信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 出库数据操作接口
     * @param req 操作对象
     * 1.提交审批 2.审批通过 3.审批退回 4.完成 5.还原
     * @return
     */
    @PostMapping("/operationOtherOutStock")
    @ResponseBody
    public R operationOtherOutStock(@RequestBody OperationReq req){
        try {
            LOGGER.info("仓库管理-其它出库-其它出库数据操作接口 req=" + req.toString());
            return otherOutStockService.operationOtherOutStock(req);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-其它出库数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取库存信息
     * @param req 查询条件
     * @return 库存信息集合
     */
    @GetMapping("/getStockList")
    @ResponseBody
    public R getStockList(StockInfoReq req){
        LOGGER.info("仓库管理-其它出库-获取库存信息 req=" + req.toString());
        try {
            List<OtherOutStockByStockVo> stockVos = otherOutStockService.getStockList(req);
            return R.build(stockVos);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-获取库存信息异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它出库新增
     * @param req 新增参数
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody AddOtherOutStockReq req){
        LOGGER.info("仓库管理-其它出库-其它出库新增 req=" + req.toString());
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return otherOutStockService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-其它出库新增异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取详情
     * @param id 出库id
     * @return
     */
    @GetMapping("/detail/getOtherOutStockDetailInfo")
    @ResponseBody
    public R getOtherOutStockDetailInfo(Integer id){
        try {
            LOGGER.info("仓库管理-出库-获取详情 id=" + id);
            OtherOutStockVo vo = otherOutStockService.getOtherOutStockDetailInfo(id, false);
            return R.build(vo);
        } catch (Exception e){
            LOGGER.info("仓库管理-出库-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 修改-获取详情
     * @param id 出库id
     * @return
     */
    @GetMapping("/detail/update")
    @ResponseBody
    public R update(Integer id){
        try {
            LOGGER.info("仓库管理-其它出库-修改-获取详情 id=" + id);
            OtherOutStockVo vo = otherOutStockService.getOtherOutStockDetailInfo(id, true);
            return R.build(vo);
        } catch (Exception e){
            LOGGER.info("仓库管理-其它出库-修改-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它出库审核
     * @param outStockId 其它出库id
     * @return 审核结果
     */
    @GetMapping("/checkOtherOutStock")
    @ResponseBody
    public R checkOtherOutStock(Integer outStockId){
        LOGGER.info("仓库管理-其它出库-其它出库审核 outStockId=" + outStockId);
        try {
            otherOutStockService.checkOtherOutStock(outStockId);
            return R.build(ResultStatusEnum.SUCCESS);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-其它出库审核异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取库存产品
     * @param req 查询参数
     * @return 库存产品对象集合
     */
    @GetMapping("/add/getStockInfoByPage")
    @ResponseBody
    public R getStockInfo(OtherOutStockProductReq req){
        LOGGER.info("仓库管理-其它出库-获取入库产品 req=" + req);
        try {
            PageInfo<OtherOutStockProductVo> productVos = otherOutStockService.getStockInfo(req);
            return R.build(0,"", productVos.getTotal(), productVos.getList());
        }catch (Exception e){
            LOGGER.info("仓库管理-调拨-获取入库产品异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 获取客户所有信息
     * @return 客户所有信息
     */
    @GetMapping("/add/getCustomerAllInfo")
    @ResponseBody
    public R getCustomerAllInfo(){
        LOGGER.info("仓库管理-其它出库-获取客户信息");
        try {
            List<CustomerInfoRsp> customerInfo = otherOutStockService.getCustomerAllInfo();
            return R.build(customerInfo);
        }catch (Exception e){
            LOGGER.info("仓库管理-调拨-获取客户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过客户编码获取客户联系人信息
     * @param customCode 客户编码
     * @return 客户联系人
     */
    @PostMapping("/add/getCustomerContactInfo")
    @ResponseBody
    public R getCustomerContactInfo(@RequestBody String customCode){
        LOGGER.info("仓库管理-其它出库-根据客户编码获取客户信息 customCode=" + customCode);
        try {
            List<CustomerContactRsp> customerContactInfo = otherOutStockService.getCustomerContactInfo(customCode);
            return R.build(customerContactInfo);
        }catch (Exception e){
            LOGGER.info("仓库管理-调拨-根据客户编码获取客户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 获取所有用户信息
     * @return 所有用户信息
     */
    @GetMapping("/getAllUserInfo")
    @ResponseBody
    public R getAllUserInfo(){
        LOGGER.info("仓库管理-其它出库-获取所有用户信息");
        try {
            List<UserInfoRsp> userList = otherOutStockService.getAllUserInfo();
            return R.build(userList);
        }catch (Exception e){
            LOGGER.info("仓库管理-其它出库-获取所有用户信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它出库产品列表统计
     *
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("/statistics/productInfo")
    @ResponseBody
    public R statisticsProductInfo(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-其它出库-其它出库产品列表统计 req=" + req);
            PageInfo<OtherOutStockProductStatisticsVo> pageInfo = otherOutStockService.statisticsProductInfo(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它出库-其它出库产品列表统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 其它出库产品列表统计-导出
     *
     * @param req 操作对象
     * @return 操作结果
     */
    @RequestMapping("/export/productInfoExport")
    @ResponseBody
    public R statisticsProductInfoExport(sellOutStockStatistics1Req req) {
        try {
            LOGGER.info("仓库管理-其它出库-其它出库产品列表统计-导出 req=" + req);
            JSONObject Info = otherOutStockService.statisticsProductInfoExport(req);
            return R.build(Info);
        } catch (Exception e) {
            LOGGER.info("仓库管理-其它出库-其它出库产品列表统计-导出异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
